﻿using Caliburn.Micro;

using Cookbook.Common;

using Cooking.Client.Querying;
using Cooking.Client.Services;
using Cooking.Client.Specs.Builders;
using Cooking.Client.Specs.Support;
using Cooking.Client.ViewModels;
using Cooking.Client.ViewModels.Coroutines;

using FakeItEasy;

using FluentAssertions;

using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cooking.Client.Specs
{
    public class SearchViewModelSpecs
    {
        [TestClass]
        [Tag("SearchViewModel")]
        public class When_initializing : Specification
        {
            private SearchViewModel model;

            protected override void EstablishContext()
            {
                model = new SearchViewModel(A.Fake<ICommandAgent>(), A.Fake<IApplicationController>());
            }

            protected override void Because()
            {
                ((IActivate)model).Activate();
            }

            [TestMethod]
            public void It_should_set_the_default_search_criteria()
            {
                model.MinimumRating.Should().BeLessOrEqualTo(0);
                model.PartialDescription.Should().BeEmpty();
                model.PartialTitle.Should().BeEmpty();
            }
        }

        [TestClass]
        [Tag("SearchViewModel")]
        public class When_adding_a_new_recipe : Specification
        {
            private SearchViewModel model;
            private IApplicationController controller;

            protected override void EstablishContext()
            {
                controller = A.Fake<IApplicationController>();

                model = new SearchViewModel(A.Fake<ICommandAgent>(), controller);
            }

            protected override void Because()
            {
                model.AddNewRecipe();
            }

            [TestMethod]
            public void It_should_show_the_corresponding_dialog()
            {
                A.CallTo(() => controller.ShowRecipeAddingDialog());
            }
         }

        [TestClass]
        [Tag("SearchViewModel")]
        public class When_searching_for_recipes : Specification
        {
            private SearchViewModel model;
            private Recipe recipe;

            protected override void EstablishContext()
            {
                recipe = new RecipeBuilder().Build();

                model = new SearchViewModel(A.Fake<ICommandAgent>(), A.Fake<IApplicationController>());
            }

            protected override void Because()
            {
                var enumerator = model.FindRecipes().GetCoroutineEnumerator();
                
                var coroutine = enumerator.Next<FindRecipesCoroutine>();
                coroutine.Recipes = recipe.ToEnumerable();
                
                enumerator.Finish();
            }

            [TestMethod]
            public void It_should_get_the_recipes_from_the_coroutine()
            {
                model.Recipes.Should().Equal(recipe);
            }
        }

        [TestClass]
        [Tag("SearchViewModel")]
        public class When_a_new_recipe_was_added_concurrenctly : Specification
        {
            protected override void EstablishContext()
            {
            }

            protected override void Because()
            {
            }

            [TestMethod]
            public void It_should_reload_the_list_of_recipes()
            {
            }
        }

        [TestClass]
        [Tag("SearchViewModel")]
        public class When_a_rating_is_changed : Specification
        {
            protected override void EstablishContext()
            {
            }

            protected override void Because()
            {
            }

            [TestMethod]
            public void It_should_submit_a_command_to_the_server()
            {
            }
        }
    }
}